import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

import java.io.File;
import java.time.Duration;

public class StartUI extends Application {
    static Image image[] = new Image[4];
    static Range range;
    static Image background= new Image("Puzzle/101.png");
    static int level;
    static MediaPlayer start;
    int pitch[];
    int pnum = 11;

    public static void main(String[] args) {
        launch();
    }

    @Override
    public void start(Stage stage) throws Exception {
        range = new Range();
        stage.setResizable(false);
        File url = new File("xuantu.mp3");
        start = new MediaPlayer(new Media(url.toURI().toString()));
        start.setCycleCount(MediaPlayer.INDEFINITE);
        start.setVolume(0.75);
        if (range.set.get(4).compareTo("1")==0) {
            start.play();
        }
        pitch = new int[4];
        setPitch();
        if (StartUI.range.set.get(6).compareTo("0") == 0) {
            level = 3;
        } else {
            level = 4;
        }
        Label label = new Label("  请选择喜欢的图片开始游戏");
        label.setFont(Font.font("宋体", FontWeight.BOLD, 17));
        String png[] = new String[4];
        Button button[] = new Button[4];
        ImageView imageView[] = new ImageView[4];
        for (int i = 0; i < 4; i++) {
            png[i] = "Puzzle/" + pitch[i] + ".png";
        }
        for (int i = 0; i < 4; i++) {
            image[i] = new Image(png[i]);
            imageView[i] = new ImageView(image[i]);
            imageView[i].setFitWidth(150);
            imageView[i].setFitHeight(150);
            button[i] = new Button("", imageView[i]);
            button[i].setPrefSize(150, 150);
        }
        button[0].setOnAction(event -> {
            new eventAction(image[0]).buttonaction.handle(event);
            stage.close();
            start.stop();
        });
        button[1].setOnAction(event -> {
            new eventAction(image[1]).buttonaction.handle(event);
            stage.close();
            start.stop();
        });
        button[2].setOnAction(event -> {
            new eventAction(image[2]).buttonaction.handle(event);
            stage.close();
            start.stop();
        });
        button[3].setOnAction(event -> {
            new eventAction(image[3]).buttonaction.handle(event);
            stage.close();
            start.stop();
        });
        Image image2 = new Image("Puzzle/902.png");
        ImageView imageView2 = new ImageView(image2);
        Button setting = new Button("", imageView2);
        setting.setOnAction(event -> {
            new eventAction().buttonaction.handle4(event);
        });
        Button ranger = new Button("排行榜");
        ranger.setFont(Font.font("宋体", FontWeight.BOLD, 17));
        ranger.setOnAction(event -> {
            new eventAction().buttonaction.handle3(event);
        });
        HBox hBox2 = new HBox();//装入设置和排行榜按键
        hBox2.getChildren().addAll(setting, ranger);
        hBox2.setSpacing(5);
        hBox2.setPadding(Insets.EMPTY);
        HBox hBox = new HBox(label, hBox2);
        hBox.setPadding(new Insets(25, 10, 15, 10));
        hBox.setSpacing(30);
        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10, 20, 30, 20));
        gridPane.setVgap(20);
        gridPane.setHgap(20);
        int m = 0;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                gridPane.add(button[m], i, j);
                m++;
            }
        }
        ImageView back = new ImageView(background);
        back.setOpacity(0.75);
        BorderPane borderPane = new BorderPane();
        borderPane.setTop(hBox);
        borderPane.setCenter(gridPane);
        StackPane stackPane = new StackPane(back, borderPane);
        Scene scene = new Scene(stackPane, 390, 440);
        stage.setScene(scene);
        stage.setTitle("拼图游戏");
        stage.show();
    }

    void setPitch() {

        for (int i = 0; i < 4; i++) {
            pitch[i] = -1;
        }
        boolean go = true;
        int i = 0;
        int m;
        //生成StartUI.level*StartUI.level-1个不重复随机数
        while (go) {
            m = (int) (Math.random() * pnum);
            if (!isContent(pitch, m)) {
                pitch[i] = m;
                i++;
                if (i == 4) {
                    go = false;
                }
            }
        }
    }

    //判断是否重复
    boolean isContent(int[] pitch, int n) {
        for (int i = 0; i < 4; i++) {
            if (pitch[i] == n) {
                return true;
            }
        }
        return false;
    }
}